<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="UTF-8" />
    <link href="../../../style.css" rel="stylesheet" type="text/css" />
    <title>预授权完成</title>
</head>
<body style="text-align:center;">
<?php



//接口版本号
$version = '1.0';
//交易类型编码
$txnType = 'CFM';
//消息状态
$interactiveStatus = 'TR1';
//商户编号  由快钱公司分配给商户的 15位唯一标识号。
$merchantId = '812330945110001';
//终端编号
$terminalId = '33090301';
//tr3回调地址  接收快钱TR3报文的地址
$tr3Url = 'http://www.baidu.com';
//商户端交易时间
$entryTime = date('YmdHis');
//信用卡卡号
$cardNo = '4392268386764048';
//交易金额
$amount = '1';
//外部检索参考号  用于商户自定义标识一笔交易
$externalRefNumber = date('YmdHis');

//原系统参考号   系统参考号(refNumber)
$origRefNumber='000013267406';

//授权码
$authorizationCode='594833';   // 交易了一块钱




?>
<h2>预授权完成</h2>
<font color="#ff0000">（该页面仅做参考）</font>
<form method=post action="" name="">
    <table cellpadding="0" width="610px" border="1" style="border-collapse: collapse" bordercolor="green" align="center">
        <tr>
            <td>接口版本号(version)<span class="">*</span></td>
            <td><?php echo $version; ?></td>
        </tr>
        <tr>
            <td>交易类型(txnType)<span class="">*</span></td>
            <td><?php echo $txnType; ?></td>
        </tr>
        <tr>
            <td>消息状态(interactiveStatus)<span class="">*</span></td>
            <td><?php echo $interactiveStatus; ?></td>
        </tr>
        <tr>
            <td>卡号(cardNo)<span class="">*</span></td>
            <td><?php echo $cardNo; ?></td>
        </tr>

        <tr>
            <td>交易金额(amount)<span class="">*</span></td>
            <td><?php echo $amount; ?></td>
        </tr>
        <tr>
            <td>商户编号(merchantId)<span class="">*</span></td>
            <td><?php echo $merchantId; ?></td>
        </tr>
        <tr>
            <td>终端编号(terminalId)<span class="">*</span></td>
            <td><?php echo $terminalId; ?></td>
        </tr>
        <tr>
            <td>商户端交易时间(entryTime)<span class="">*</span></td>
            <td><?php echo $entryTime; ?></td>
        </tr>

        <tr>
            <td>外部跟踪编号(externalRefNumber)<span class="">*</span></td>
            <td><?php echo $externalRefNumber; ?></td>
        </tr>
        <tr>
            <td>tr3 回调地址  (tr3Url)<span class=""></span></td>
            <td><?php echo $tr3Url; ?></td>
        </tr>
        <tr>
            <td>tr3 原系统参考号  (origRefNumber)<span class=""></span></td>
            <td><?php echo $origRefNumber; ?></td>
        </tr>
        <tr>
            <td>tr3 授权码  (authorizationCode)<span class=""></span></td>
            <td><?php echo $authorizationCode; ?></td>
        </tr>

        <tr>
            <td colspan="2" style="text-align: center">
                <input type="submit" value="预授权 完成" name="pre_trans" style="font-size:x-large;height:34px;width:177px"/>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

<?PHP

if($_POST[pre_trans]){


    function kq_ck_null($kq_va,$kq_na)
    {
        if($kq_va == ""){$kq_va="";
        }else
        {return '<'.$kq_na.'>'.$kq_va.'</'.$kq_na.'>';
        }
    }

    $tr3Url    =kq_ck_null($tr3Url,'tr3Url');

    $reqXml = '<?xml version="1.0" encoding="UTF-8"?><MasMessage xmlns="http://www.99bill.com/mas_cnp_merchant_interface"><version>'.$version.'</version><TxnMsgContent><txnType>'.$txnType.'</txnType><interactiveStatus>'.$interactiveStatus.'</interactiveStatus><cardNo>'.$cardNo.'</cardNo><amount>'.$amount.'</amount><merchantId>'.$merchantId.'</merchantId><terminalId>'.$terminalId.'</terminalId><entryTime>'.$entryTime.'</entryTime><externalRefNumber>'.$externalRefNumber.'</externalRefNumber><origRefNumber>'.$origRefNumber.'</origRefNumber> <authorizationCode>'.$authorizationCode.'</authorizationCode>'.$tr3Url.'</TxnMsgContent></MasMessage>';

//echo '$regXml: '.$reqXml.'<HR>';

    $clientcert    ="/home/flightsharecar/user_upload/php_demo/cert.pem";
    $clientprikey="/home/flightsharecar/user_upload/php_demo/private.pem";

//$challenge      = "vpos123";
    $tuCurl = curl_init();
    $merchantId='812330945110001';//商户编号
    $certPassword='vpos123';//证书密码
    $loginKey='vpos123';//商户登录密码
    $loginInfo = array(
        "Authorization: Basic " . base64_encode($merchantId . ":" . $loginKey)
    );
    curl_setopt($tuCurl, CURLOPT_URL, "https://sandbox.99bill.com:9445/cnp/purchase");
// Set your login and password for authentication
    /*
curl_setopt($tuCurl, CURLOPT_USERPWD, 'vpos123');
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $loginInfo);
curl_setopt($tuCurl, CURLOPT_SSLCERT, $clientcert);
curl_setopt($tuCurl, CURLOPT_SSLCERTPASSWD, $challenge);
curl_setopt($tuCurl, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $reqXml);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
    */
    curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $loginInfo);//请求头中包含merchantId和对应的密码的内容
    //curl_setopt($tuCurl, CURLOPT_USERPWD, $loginKey);//登录用户密码

    curl_setopt($tuCurl,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($tuCurl,CURLOPT_SSL_VERIFYHOST,false);


    curl_setopt($tuCurl,CURLOPT_SSLCERTTYPE,'PEM');//证书格式
    curl_setopt($tuCurl,CURLOPT_SSLCERT,$clientcert);//证书路径
    curl_setopt($tuCurl,CURLOPT_SSLCERTPASSWD,$certPassword);//证书密码

    curl_setopt($tuCurl,CURLOPT_SSLKEYTYPE,'PEM');//本地私钥格式
    curl_setopt($tuCurl,CURLOPT_SSLKEY,$clientprikey);//私钥文件路径

    curl_setopt($tuCurl, CURLOPT_POST, 1);//POST
    curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $reqXml);//POST内容中包含报文


    curl_setopt($tuCurl,CURLOPT_TIMEOUT,30);

    curl_setopt($tuCurl,CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch,CURLOPT_URL,$url);

    $tuData=curl_exec($tuCurl);

    var_dump($tuData);
    if(!curl_errno($tuCurl)){

        $info = curl_getinfo($tuCurl);

        // echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
    } else {

        echo 'Curl error: ' . curl_error($tuCurl);
    }
    echo '<HR><HR>';
    echo '<h2>返回TR2通知信息</h2>';
    eregi("<txntype>(.*)<\/txntype>", $tuData, $txntype);
    eregi("<interactiveStatus>(.*)<\/interactiveStatus>", $tuData, $interactiveStatus);
    eregi("<amount>(.*)<\/amount>", $tuData, $amount);
    eregi("<merchantId>(.*)<\/merchantId>", $tuData, $merchantId);
    eregi("<terminalId>(.*)<\/terminalId>", $tuData, $terminalId);
    eregi("<entryTime>(.*)<\/entryTime>", $tuData, $entryTime);
    eregi("<externalRefNumber>(.*)<\/externalRefNumber>", $tuData, $externalRefNumber);
    eregi("<transTime>(.*)<\/transTime>", $tuData, $transTime);
    eregi("<refNumber>(.*)<\/refNumber>", $tuData, $refNumber);
    eregi("<responseCode>(.*)<\/responseCode>", $tuData, $responseCode);
    eregi("<responseTextMessage>(.*)<\/responseTextMessage>", $tuData, $responseTextMessage);
    eregi("<cardOrg>(.*)<\/cardOrg>", $tuData, $cardOrg);
    eregi("<issuer>(.*)<\/issuer>", $tuData, $issuer);
    eregi("<storableCardNo>(.*)<\/storableCardNo>", $tuData, $storableCardNo);
    eregi("<authorizationCode>(.*)<\/authorizationCode>", $tuData, $authorizationCode);
    eregi("<origRefNumber>(.*)<\/origRefNumber>", $tuData, $origRefNumber);

    echo '<table align="center" bordercolor="green" border="1" style="border-collapse: collapse;width:610px">';
    echo '<tr><td>交易类型(txntype)</td><td>'.$txntype[0].'</td></tr>';
    echo '<tr><td>消息状态(interactiveStatus)</td><td>'.$interactiveStatus[0].'</td></tr>';
    echo '<tr><td>金额(amount)</td><td>'.$amount[0].'</td></tr>';
    echo '<tr><td><font color="#ff0000">商户号(merchantId)</font></td><td>'.$merchantId[0].'</td></tr>';
    echo '<tr><td><font color="#ff0000">终端号(terminalId)</font></td><td>'.$terminalId[0].'</td></tr>';
    echo '<tr><td>商户端交易时间(entryTime)</td><td>'.$entryTime[0].'</td></tr>';
    echo '<tr><td><font color="#ff0000">外部跟踪编号(externalRefNumber)</font></td><td>'.$externalRefNumber[0].'</td></tr>';
    echo '<tr><td>交易传输时间(transTime)</td><td>'.$transTime[0].'</td></tr>';
    echo '<tr><td>系统参考号(refNumber)</td><td>'.$refNumber[0].'</td></tr>';
    echo '<tr><td><font color="#ff0000">应答码(responseCode)</font></td><td>'.$responseCode[0].'</td></tr>';
    echo '<tr><td><font color="#ff0000">应答码文本消息(responseTextMessage)</font></td><td>'.$responseTextMessage[0].'</td></tr>';
    echo '<tr><td>卡组织编号(cardOrg)</td><td>'.$cardOrg[0].'</td></tr>';
    echo '<tr><td>发卡银行名称(issuer)</td><td>'.$issuer[0].'</td></tr>';
    echo '<tr><td>缩略卡号(storableCardNo)</td><td>'.$storableCardNo[0].'</td></tr>';
    echo '<tr><td>授权码(authorizationCode)</td><td>'.$authorizationCode[0].'</td></tr>';
    echo '<tr><td>原系统参考号(origRefNumber)</td><td>'.$origRefNumber[0].'</td></tr>';

}
?>

</body>
</html>